import express from 'express';
import { formatError, formatSuccess } from '../utils/helpers.js';

export function createGamesRouter(gameEngine) {
  const router = express.Router();

  // 获取游戏状态
  router.get('/:roomId/state', async (req, res) => {
    try {
      const { roomId } = req.params;
      const gameState = await gameEngine.getGameState(roomId);
      
      if (!gameState) {
        return res.status(404).json(formatError('GAME_NOT_FOUND', '游戏不存在'));
      }

      res.json(formatSuccess(gameState, '获取游戏状态成功'));
    } catch (error) {
      res.status(500).json(formatError('SERVER_ERROR', '服务器内部错误'));
    }
  });

  // 获取游戏统计
  router.get('/stats', (req, res) => {
    try {
      const stats = gameEngine.getStats();
      res.json(formatSuccess(stats, '获取游戏统计成功'));
    } catch (error) {
      res.status(500).json(formatError('SERVER_ERROR', '服务器内部错误'));
    }
  });

  return router;
}